package com.is.add.util.DimensionUtil;

import com.is.add.model.OrderBase;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * 统计工具类
 * Created by liuming on 2016/5/5.
 */
public class StatisticMaster {
    public static DataCount loadResult(List<OrderBase> baseList, List<Dimension<OrderBase>> dimensions){
        DimensionData<OrderBase> dd=new DimensionData<>(dimensions);
        dd.addBaseList(baseList);
        dd.doStatistic();
        return doSum(dd,"-总计");
    }

    /**
     * 递归获得结果集
     * @param dd        维度数据集
     * @param title     维度Key
     * @return          统计结果集
     */
    private static DataCount doSum(DimensionData<OrderBase> dd, String title){
        //根据标题(key)创建结果对象
        DataCount s=new DataCount(title);
        //获得维度数据集中的维度值映射对象
        LinkedHashMap<String,DimensionData<OrderBase>> ddMap= dd.getDataMap();
        //如果该对象不为空则存在子维度，循环其中的每个维度数据集，并创建结果对象进行递归
        if(ddMap!=null&&ddMap.size()>0){
            for(String key:ddMap.keySet()){
                DimensionData<OrderBase> subDd=ddMap.get(key);
                //递归获得子结果集
                DataCount subSettle=StatisticMaster.doSum(subDd,key);
                //添加子结果集到当前结果集中
                s.getSubSettlements().add(subSettle);
            }
            //根据当前结果集中的子结果集计算统计数据
            s.doSum();
        }
        else{//最底层维度直接计算基础Bean
            List<OrderBase> baseList= dd.getBaseList();
            for(OrderBase base:baseList){
                s.countAppend(1)
                 .paySumAppend(base.getPaySum())
                 .orderNumAppend(base.getOrderNum())
                 .feeAppend(base.getFee())
                 .subsidyFeeAppend(base.getSubsidyFee())
                 .subsidySumAppend(base.getSubsidySum());
            }
            //s.setBaseList(baseList);
        }
        return s;
    }
}
